package com.qiandw.activiti;

import com.qiandw.dao.LeaveBillDao;
import com.qiandw.entity.LeaveBill;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假流程实现类
 *
 * @author KG
 */
@Service("leaveService")
public class LeaveService {
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    @Qualifier("leaveBillDao")
    private LeaveBillDao leaveBillDao;

    /**
     * 启动请假流程
     *
     * @param bizId 业务id（谁来请假）
     */
    public void startProcess(String bizId) {
        runtimeService.startProcessInstanceByKey("leaveProcess", bizId);
    }

    /**
     * 查询相关审批人
     *
     * @param userId userId
     * @return
     */
    public List<Task> findTaskByUserId(String userId) {
        return taskService.createTaskQuery().taskCandidateOrAssigned(userId).list();
    }

    /**
     * 审批任务
     *
     * @param taskId 任务id(表示审批那个任务)
     * @param userId 谁来审批
     * @param audit  审批意见 pass,reject
     */
    public void completeTaskByUser(String taskId, String userId, String audit) {
        //认领任务
        taskService.claim(taskId, userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("audit", audit);
        //完成任务
        taskService.complete(taskId, map);
    }

    /**
     * 修改请假流程单状态
     *
     * @param execution 执行实例对象，代表一个请假的具体实例
     * @param status    请假单状态
     */
    public void changeLeaveBillStatus(DelegateExecution execution, String status) {
        String businessKey = execution.getProcessInstanceBusinessKey();
        LeaveBill leaveBill = leaveBillDao.findOne(businessKey);
        leaveBill.setStatus(status);
        leaveBillDao.save(leaveBill);
    }

    /**
     * 查询相关的项目经理
     *
     * @return
     */
    public List<String> findProjectManger() {
        return Arrays.asList("projectManger1", "projectManger2");
    }

    /**
     * 查询相关的人事经理
     *
     * @return
     */
    public List<String> findHR() {
        return Arrays.asList("HR1", "HR2");
    }
}
